/**
 * 
 */
package com.cplsystems.incidencias.vm;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.image.Image;
import org.zkoss.zk.ui.select.annotation.VariableResolver;

import com.cplsystems.incidencias.app.MetaData;
import com.cplsystems.incidencias.app.utils.ImageUtils;
import com.cplsystems.incidencias.app.utils.IncidenciasConstants;
import com.cplsystems.incidencias.app.utils.IncidenciasUtils;
import com.cplsystems.incidencias.app.utils.SessionUtils;
import com.cplsystems.incidencias.business.Organizacion;

/**
 * @author César Palalía López (csr.plz@aisa-automation.com)
 * 
 */
@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class MenuVM extends MenuVariables {

	private static final long serialVersionUID = -2290644178316110165L;
	public static final String PAGE_TO_RENDER = "pageToRender";
	private Map<String, Object> args;

	protected Image businessImage;
	protected byte[] imageInBytes;
	protected String imageFormat;	
	
	@Init
	public void init() {
		args = new HashMap<String, Object>();
		loadCompanyDetails();
		
		Organizacion org = (Organizacion) sessionUtils
				.getFromSession(IncidenciasUtils
						.encrypt(IncidenciasConstants.FIRMA));
		
		if(org != null && org.isPermiso() == true)
			subMenusVisibles = true;
		
		
	}

	private void loadCompanyDetails() {
		Organizacion organizacion = (Organizacion) sessionUtils
				.getFromSession(IncidenciasUtils
						.encrypt(IncidenciasConstants.FIRMA));
		if (organizacion != null) {
			try {
				if (organizacion.getImagenPerfil() == null) {
					return;
				}
				File picture = new File(organizacion.getImagenPerfil());
				imageInBytes = incidenciasUtils.readFileToByteArray(picture);
				businessImage = ImageUtils.scaleToSize(imageInBytes, 250, 400,
						"png").getContent();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			
	}

	@Command
	public void showEmpresa() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.EMPRESA);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showProducts()");
	}

	@Command
	public void showTrabajadores() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.TRABAJADORES);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showProviders()");
	}

	@Command
	public void showIMSS() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.IMMS);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showRequisitions()");
	}

	@Command
	public void showReports() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.REPORTS);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showReports()");
	}

	@Command
	public void showControlPanel() {
		args.put(PAGE_TO_RENDER,
				IncidenciasConstants.GLOBAL_PAGES.CONTROL_PANEL);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showControlPanel()");
	}

	@Command
	public void showIncidencias() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.INCIDENCIAS);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showReports()");
	}

	@Command
	public void showCuentas() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.CUENTAS);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showReports()");
	}

	@Command
	public void showCatalogos() {
		args.put(PAGE_TO_RENDER, IncidenciasConstants.GLOBAL_PAGES.CATALOGOS);
		BindUtils.postGlobalCommand(null, null, "updateWorkArea", args);
		System.out.println("MenuVM.showReports()");
	}

	public Image getBusinessImage() {
		return businessImage;
	}

	public void setBusinessImage(Image businessImage) {
		this.businessImage = businessImage;
	}

}
